package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.vehicle.bean.Contact;
import com.vehicle.form.ContactForm;
import com.vehicle.mapper.ContactMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.service.ContactService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-20
 */
@Service
public class ContactServiceImpl extends ServiceImpl<ContactMapper, Contact> implements ContactService {

    @Autowired
    ContactMapper contactMapper;

    @Override
    public Integer insertContact(ContactForm contactForm) {
        Contact contact = new Contact();
        BeanUtils.copyProperties(contactForm,contact);
        int b = contactMapper.insert(contact);
        if(b==1){
            return contact.getContactId();
        }
        return -1;
    }

    @Override
    public ContactForm getContactById(Integer contactId) {
        QueryWrapper<Contact> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contact_id",contactId);
        Contact contact = contactMapper.selectOne(queryWrapper);
        System.out.println("这是contact"+contact);
        ContactForm contactForm = new ContactForm();
        BeanUtils.copyProperties(contact,contactForm);
        System.out.println("这是contactForm"+contactForm);
        return contactForm;
    }

    @Override
    public Boolean updateTheContact(ContactForm contactForm) {
        UpdateWrapper<Contact> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("contact_id",contactForm.getContactId());
        if(contactForm.getContactName()!=null){
            updateWrapper.set("contact_name",contactForm.getContactName());
        }
        if(contactForm.getContactPhone()!=null){
            updateWrapper.set("contact_phone",contactForm.getContactPhone());
        }
        if(contactForm.getContactRelation()!=null){
            updateWrapper.set("contact_relation",contactForm.getContactRelation());
        }
        int b = contactMapper.update(null,updateWrapper);
        if(b==1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteContactById(Integer contactId) {
        int i = contactMapper.deleteById(contactId);
        if(i==1){
            return true;
        }
        return false;
    }
}
